﻿using System;
using System.IO;
using System.Web.Mvc;

namespace CodeContrib.Web.Mvc.Html
{
	public class MvcTable : IDisposable
	{
		private bool _isDisposed;
		private readonly TextWriter _writer;

		public MvcTable(ViewContext viewContext)
		{
			Guard.NotNull(viewContext, "viewContext");

			_writer = viewContext.Writer;
		}

		public void Dispose()
		{
			Dispose(true /* disposing */);

			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!_isDisposed && disposing)
			{
				_writer.Write("</table>");
			}

			_isDisposed = true;
		}

		public void EndForm()
		{
			Dispose(true /* disposing */);
		}
	}
}